package educrypt.gui.util;

import java.awt.Color;
import java.awt.Component;
import java.awt.Font;
import java.awt.Frame;
import java.awt.GraphicsEnvironment;
import java.awt.Window;
import java.util.Enumeration;
import java.util.List;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.UIManager;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;

import educrypt.i18n.Messages;
import educrypt.log.SimpleLogger;
import educrypt.resources.EduCryptResources;

public class GuiUtil {
	public static SimpleAttributeSet formatConsoleMessage(int type) {
		SimpleAttributeSet attrs = new SimpleAttributeSet();

		switch (type) {
		case SimpleLogger.TITLE:
			StyleConstants.setBold(attrs, true);
			StyleConstants.setForeground(attrs, Color.BLACK);
			break;
		case SimpleLogger.CONTENT:
			StyleConstants.setBold(attrs, false);
			StyleConstants.setForeground(attrs, Color.DARK_GRAY);
			break;
		case SimpleLogger.INFO:
			StyleConstants.setBold(attrs, false);
			StyleConstants.setForeground(attrs, Color.BLACK);
			break;
		case SimpleLogger.ERROR:
			StyleConstants.setBold(attrs, false);
			StyleConstants.setForeground(attrs, Color.RED);
			break;
		case SimpleLogger.SUCCESS:
			StyleConstants.setBold(attrs, false);
			StyleConstants.setForeground(attrs, Color.GREEN);
			break;
		}
		return attrs;
	}

	public static void setTransparentButtonBackground(JButton btn) {
		btn.setOpaque(false);
		btn.setContentAreaFilled(false);
		btn.setBorderPainted(false);
	}

	public static void setDefaultFont() {
		Font f = null;
		try {
			f = Font.createFont(Font.TRUETYPE_FONT,
					EduCryptResources.getFontStream()).deriveFont(12f);

			// This allow to use the font into JLabel with HTML
			GraphicsEnvironment genv = GraphicsEnvironment
					.getLocalGraphicsEnvironment();
			genv.registerFont(f);
		} catch (Exception e) {
			e.printStackTrace();
		}

		// UIManager.put("OptionPane.font", f);
		// UIManager.put("OptionPane.messsageFont", f);
		// UIManager.put("OptionPane.buttonFont", f);
		Enumeration<Object> keys = UIManager.getDefaults().keys();
		while (keys.hasMoreElements()) {
			Object key = keys.nextElement();
			Object value = UIManager.get(key);
			if (value instanceof javax.swing.plaf.FontUIResource) {
				UIManager.put(key, f);

			}
		}
	}

	public static void setComponentFontProperties(Component c, int style,
			float size) {
		setComponentFontSize(c, size);
		setComponentFontStyle(c, style);
	}

	public static void setComponentFontSize(Component c, float size) {
		c.setFont(c.getFont().deriveFont(size));
	}

	public static void setComponentFontStyle(Component c, int style) {
		c.setFont(c.getFont().deriveFont(style));
	}

	public static void addItemsMaintainingSelected(JComboBox cbx,
			List<String> elements) {
		// Backing up last selected item
		Object lastSelection = cbx.getSelectedItem();
		// Setting new items
		addItems(cbx, elements);
		// Trying to select the same item
		if (lastSelection != null) {
			cbx.setSelectedItem(lastSelection);
		}
	}

	public static void addItems(JComboBox cbx, List<String> elements) {
		cbx.removeAllItems();
		for (String element : elements) {
			cbx.addItem(element);
		}
	}

	public static Window findParentWindow(Component c) {
		if (c == null) {
			return JOptionPane.getRootFrame();
		} else if (c instanceof Window) {
			return (Window) c;
		} else {
			return findParentWindow(c.getParent());
		}
	}

	public static Frame findParentFrame(Component c) {
		if (c == null) {
			return JOptionPane.getRootFrame();
		} else if (c instanceof JFrame) {
			return (Frame) c;
		} else {
			return findParentFrame(c.getParent());
		}
	}

	public static void showExceptionDialog(Component parent, String message) {
		ImageIcon exceptionIcon = new ImageIcon(
				EduCryptResources.getImageURL("exception64.png"));
		JOptionPane.showMessageDialog(parent, message,
				Messages.getString("exception"),
				JOptionPane.INFORMATION_MESSAGE, exceptionIcon);
	}

	public static void showExceptionDialog(Component parent) {
		ImageIcon exceptionIcon = new ImageIcon(
				EduCryptResources.getImageURL("exception64.png"));
		JOptionPane.showMessageDialog(parent,
				Messages.getString("genericException"),
				Messages.getString("exception"),
				JOptionPane.INFORMATION_MESSAGE, exceptionIcon);
	}

	public static int showQuestionDialog(Component parent, String title,
			String message) {
		ImageIcon questionIcon = new ImageIcon(
				EduCryptResources.getImageURL("question64.png"));
		return JOptionPane.showConfirmDialog(parent, message, title,
				JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE,
				questionIcon);

	}
}
